**********************************************************************
**********************************************************************
*********************** THIS IS THE DO FILE 9 ************************
****************** THIS FILE CREATES THE INDICES *********************
**********************************************************************
**********************************************************************


*Version: 2023 March 29


local date "$date"

cap mkdir `"$output/temp"'
cap mkdir `"$output/log/9_indices"'
cap mkdir `"$output/indices"'

	
cap log close 
log using `"$output/log/9_indices/9_indices.smcl"', replace 

di "This run uses code version from 2023/03/29"


**********************************************************************
**********************************************************************
*************************** CIP INDICES ******************************
**********************************************************************
**********************************************************************


use `"$output/data/regression_data.dta"', clear 

// gen pcp physician 
gen pcp = 0 
replace pcp = 1 if spec_code == 2 | spec_code == 5

destring COUNTY, gen(FIPS)

global CIP_RHS_index = `"i(1/2).pay_type_pooled#ibn.state i(1/2).pay_type_pooled#c.RVU i(1/2).pay_type_pooled#ibn.size_indicator patient_age charlson i.year"'
global CIP_RHS_index_inc = `"i(1/2).pay_type_pooled#ibn.state i(1/2).pay_type_pooled#c.RVU i(1/2).pay_type_pooled#ibn.size_indicator patient_age charlson hh_mean_inc i.year"'
global RHS_index_probit = `"patient_age charlson i.year"'
global CIP_RHS_index_pcp = `"i(1/2).pay_type_pooled#ibn.state#ibn.pcp i(1/2).pay_type_pooled#c.RVU i(1/2).pay_type_pooled#ibn.size_indicator patient_age charlson i.year"'


// generate empty variables to save estimates later
local version `" "" "_rf" "' // version with and without model estimates 

foreach v of local version { 
	
	gen lev_CIP_FE`v' = .
	gen lev_CIP_FE_SE`v' = .
	
	gen lev_CIP_FE_nofe`v' = .
	gen lev_CIP_FE_SE_nofe`v' = .
	
}

gen lev_CIP_FE_dx = .
gen lev_CIP_FE_SE_dx = .

gen lev_CIP_FE_fv = .
gen lev_CIP_FE_SE_fv = .

gen lev_CIP_FE_fve = .
gen lev_CIP_FE_SE_fve = .

gen lev_CIP_FE_preg = .
gen lev_CIP_FE_SE_preg = .

gen lev_CIP_FE_inc = .
gen lev_CIP_FE_SE_inc = .

gen lev_CIP_FE_icd9 = .
gen lev_CIP_FE_SE_icd9 = .

gen lev_CIP_FE_W = .
gen lev_CIP_FE_W_SE = .

gen lev_CIP_FE_nofe_fv = .
gen lev_CIP_FE_nofe_SE_fv = .

gen lev_CIP_FE_nofe_fve = .
gen lev_CIP_FE_nofe_SE_fve = .

gen lev_CIP_FE_nofe_preg = .
gen lev_CIP_FE_nofe_SE_preg = .

gen lev_CIP_FE_nofe_inc = .
gen lev_CIP_FE_nofe_SE_inc = .

gen lev_CIP_FE_W_nofe = .
gen lev_CIP_FE_W_SE_nofe = .

gen lev_CIP_FE_nofe_icd9 = .
gen lev_CIP_FE_nofe_SE_icd9 = .

gen v_initial_den_CIP = .
gen v_initial_den_CIP_SE = .

gen CIP_abs = .
gen CIP_abs_SE = .
gen CIP_abs_nofe = .
gen CIP_abs_SE_nofe = .

lab var lev_CIP_FE "State CIP Index w/ Phys FE size contr."
lab var lev_CIP_FE_SE "State CIP Index SE w/ Phys FE size contr."
lab var lev_CIP_FE_dx "State CIP Index w/ Diagnosis FE"
lab var lev_CIP_FE_SE_dx "State CIP Index SE  w/ Phys FE and Diagnosis FE"	
lab var lev_CIP_FE_fv "State CIP Index w/ Phys FE (First Visit only)"
lab var lev_CIP_FE_SE_fv "State CIP Index SE w/ Phys FE (First Visit only)"
lab var lev_CIP_FE_fve "State CIP Index w/ Phys FE (First Visit Excluded)"
lab var lev_CIP_FE_SE_fve "State CIP Index SE  w/ Phys FE (First Visit excl.)"	
lab var v_initial_den_CIP "Denial Rate Index (Initial Denial), phys FE"
lab var CIP_abs "State CIP Index in dollar and levels"
lab var CIP_abs_nofe "State CIP Index in dollar and levels (Heckmann cor)"

lab var pay_type_pooled "1: Medicare, 2: Medicaid, 3: Commercial"


************************************************
***** Generate Pregnant Patient Identifier *****
************************************************

gen pregnant_tmp = 0 
	
forval i = 1/3 {
	
	gen diagnosis2_`i' = substr(dx`i'_code, 1, 2)
	gen diagnosis3_`i' = substr(dx`i'_code, 1, 3)
	replace pregnant_tmp = 1 if inlist(diagnosis2_`i', "63", "64", "65", "66", "67")  & date_of_service <= 20361 
	replace pregnant_tmp = 1 if inlist(diagnosis3_`i', "V22", "V23", "V24", "V27", "V28") & date_of_service <= 20361 
	replace pregnant_tmp = 1 if inlist(diagnosis3_`i', "Z34", "Z36", "Z32", "Z33", "Z37", "Z38", "Z39") & date_of_service > 20361 
	replace pregnant_tmp = 1 if inlist(diagnosis2_`i', "O0", "O1", "O2", "O3", "O4", "O6" "O7", "O8", "O9") & date_of_service > 20361 

	}
	
	
bys idw_patient_id: gegen sum_pregnant = sum(pregnant_tmp) 
gen preg_pat = 0
replace preg_pat = 1 if sum_pregnant >= 1 
label var preg_pat "Indicator for pregnant only patients subsample"


****************************************************************
***** Generate Indicator for Physician/Patient Interaction *****
****************************************************************

sort idw_provider_id idw_patient_id date_of_service
by idw_provider_id idw_patient_id date_of_service: gen interaction_pat_phy = 1 if _n==1 
by idw_provider_id idw_patient_id: replace interaction_pat_phy = sum(interaction_pat_phy)
gen first_visit = 0
replace first_visit = 1 if interaction_pat_phy == 1
label var interaction_pat_phy "Number of observed physician patient interaction"


*************************************
***** Merge Mean Zipcode Income *****
*************************************
cap drop _merge 
rename zipcode zipcode_str
merge m:1 zipcode_str using `"$zip_input/$zip_code_income"'
drop if _merge == 2 
drop _merge 


*****************************************
***** Generate Heckmann First Stage *****
*****************************************
//merge variables for first stage  
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

//create variables
gen share_medicaid_elig = mean_number_mediciad_elig/county_pop
gen medicaid_visit = 0 
replace medicaid_visit = 1 if pay_type_pooled == 2 

//estimate probit 
probit medicaid_visit share_medicaid_elig $RHS_index_probit
	
predict probitxb, xb
gen pdf = normalden(probitxb)
gen cdf = normal(probitxb)
gen imr = pdf/cdf


**************************************
**************************************
****** FUNCTIONAL FORM \tau **********
**************************************
**************************************


********************
**** Unweighted ****
********************
	
foreach v of local version { 
	
	sum tau`v', d 
	local com_mean `r(mean)' //the indices will be normalized 
	
	// CPU index regression w/ phys FE
	_regress tau`v' $CIP_RHS_index, absorb(idw_provider_id)

	forvalues x = 1/2 {
				
		forvalues y = 1/51 {
				
			cap replace lev_CIP_FE`v'       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
			cap replace lev_CIP_FE_SE`v'    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
			
		}
	}
	
	
	// CPU index regression w/o FE but with Heckmann correction 
	reg tau`v' $CIP_RHS_index imr

	forvalues x = 1/2 {
				
		forvalues y = 1/51 {
				
			cap replace lev_CIP_FE_nofe`v'       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
			cap replace lev_CIP_FE_SE_nofe`v'    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
			
		}
	}

}


sum tau, d 
local com_mean `r(mean)'
// CPU index regression w/ phys FE, first visit only
_regress tau $CIP_RHS_index if first_visit == 1, absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_fv       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_SE_fv    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


// CPU index regression w/ phys FE, first visit excluded
_regress tau $CIP_RHS_index if first_visit == 0 , absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_fve       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_SE_fve    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

	
// CPU index regression w/ phys FE, subsample of pregnant patients 
_regress tau $CIP_RHS_index if preg_pat == 1 , absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_preg       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_SE_preg    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}



// CPU index regression w/ phys FE, with additional zipcode income control 
_regress tau $CIP_RHS_index_inc , absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_inc       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_SE_inc    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


	
	
	
// CPU index regression excluding the time in which ICD 10 codes were used 
_regress tau $CIP_RHS_index if date_of_service <= 20361, absorb(idw_provider_id)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_icd9       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_SE_icd9    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

	
	
// CPU index regression w/o phys FE, first visit only
reg tau $CIP_RHS_index imr  if first_visit == 1

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_nofe_fv       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_nofe_SE_fv    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


// CPU index regression w/o phys FE, first visit excluded
reg tau $CIP_RHS_index imr if first_visit == 0 

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_nofe_fve       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_nofe_SE_fve    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

	
// CPU index regression w/o phys FE, subsample of pregnant patients 
reg tau $CIP_RHS_index imr if preg_pat == 1 

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_nofe_preg       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_nofe_SE_preg    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}



// CPU index regression w/o phys FE, with additional zipcode income control 
reg tau $CIP_RHS_index_inc imr

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_nofe_inc       =    _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_nofe_SE_inc    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}
	
	
	
// CPU index regression excluding the time in which ICD 10 codes were used, w/o phys FE
reg tau $CIP_RHS_index imr if date_of_service <= 20361


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace lev_CIP_FE_nofe_icd9       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
		cap replace lev_CIP_FE_nofe_SE_icd9    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


	

******************
**** Weighted ****
******************	
	
foreach v of local version { 

	
	tabstat tau`v' [aw=pi], stat(mean) save
	matrix stats = r(StatTotal)
	local com_mean_w = stats[1,1]
	di `com_mean_w'
	
	// CPU index regression w/ phys FE, size control and Heckmann correction
	_regress tau`v' $CIP_RHS_index [aw=pi], absorb(idw_provider_id)

	forvalues x = 1/2 {
				
		forvalues y = 1/51 {
				
			cap replace lev_CIP_FE_W`v'       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_w' if pay_type_pooled == `x' & state == `y'
			cap replace lev_CIP_FE_W_SE`v'    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
			
		}
	}
	
	// CPU index regression w/o FE but with Heckmann corection 
	reg tau`v' $CIP_RHS_index imr [aw=pi]

	forvalues x = 1/2 {
				
		forvalues y = 1/51 {
				
			cap replace lev_CIP_FE_W_nofe`v'       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_w' if pay_type_pooled == `x' & state == `y'
			cap replace lev_CIP_FE_W_SE_nofe`v'    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
			
		}
	}
	

}


*****************************
**** Denial Rate Indices ****
*****************************
// Denial Rate Index -- Initial Denial Rate (Physician FE)
_regress visit_initial_den $CIP_RHS_index, absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace v_initial_den_CIP      =   _b[`x'.pay_type_pooled#`y'.state] + _b[_cons] if pay_type_pooled == `x' & state == `y'
		cap replace v_initial_den_CIP_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


*****************************
**** CIP Index in Levels ****
*****************************
// CIP index in level, unweighted with physician FE
_regress CIP $CIP_RHS_index, absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace CIP_abs      =   _b[`x'.pay_type_pooled#`y'.state] + _b[_cons] if pay_type_pooled == `x' & state == `y'
		cap replace CIP_abs_FE   =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

// CIP index in levels, unweighted, with Heckmann correction 
_regress CIP imr $CIP_RHS_index 

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace CIP_abs_nofe      =   _b[`x'.pay_type_pooled#`y'.state] + _b[_cons] if pay_type_pooled == `x' & state == `y'
		cap replace CIP_abs_SE_nofe   =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


//generate raw means 
bys pay_type_pooled payee_state_cd: gegen mean_tau = mean(tau)
bys pay_type_pooled payee_state_cd: gegen mean_CIP = mean(CIP)
bys pay_type_pooled payee_state_cd: gegen mean_denial_rate = mean(visit_initial_den)

bys payee_state_cd pay_type_pooled: keep if _n == 1 
keep  *CIP_FE* payee_state_cd state pay_type_pooled v_*_den_CIP* CIP_abs* mean_??? mean_denial_rate

save `"$output/indices/CIP_indices.dta"', replace 


keep if pay_type_pooled == 2 // keep medicaid
bys payee_state_cd: keep if _n == 1 
save `"$output/indices/CIP_indices_medicaid.dta"', replace 



**********************************************************************
**********************************************************************
*************************** FEE INDICES ******************************
**********************************************************************
**********************************************************************


use `"$output/data/$line_file"', clear 

********************************************************
****** Fee Index at Line Level from Main Dataset *******
********************************************************

// gen pcp physician 
gen pcp = 0 
replace pcp = 1 if spec_code == 2 | spec_code == 5
 
//gen final variables
gen pi_W_FE_cpt_line = .
gen pi_W_FE_cpt_line_SE = .

gen pi_FE_cpt_line = .
gen pi_FE_cpt_line_SE = .

gen pi_c_W_FE_cpt_line = .
gen pi_c_W_FE_cpt_line_SE = .

gen pi_c_FE_cpt_line = .
gen pi_c_FE_cpt_line_SE = .

gen pi_c_FE_cpt_line_abs = .
gen pi_c_FE_cpt_line_abs_SE = .

gen pi_c_FE_cpt_line_pcp = .
gen pi_c_FE_cpt_line_pcp_SE = .

gen pi_c_W_FE_cpt_line_pcp = .
gen pi_c_W_FE_cpt_line_pcp_SE = .

lab var pi_W_FE_cpt_line "Weighted Fee Index with imputations"
lab var pi_FE_cpt_line "unweighted Fee Index with imputations"
lab var pi_c_W_FE_cpt_line "Weighted Fee Index without imputations"
lab var pi_c_FE_cpt_line "Unweighted Fee Index without imputations"
lab var pi_c_FE_cpt_line_pcp "Unweighted Fee Index without imputations by pcp/not pcp"
lab var pi_c_W_FE_cpt_line_pcp "Weighted Fee Index without imputations by pcp/not pcp"
lab var pi_c_FE_cpt_line_abs "Unweighted Fee Index without imputations in levels"

//gen variables/specify controls for index computation
global RHS_fee_index = `"i(1/2).pay_type_pooled#ibn.state patient_age charlson i.year"'
global RHS_spec_fee_index = `"i(1/2).pay_type_pooled#ibn.state#ibn.pcp patient_age charlson i.year"'
gen ln_line_item_value = log(line_item_value)

//commercial fee mean weighted
tabstat ln_line_item_value [aw=meanallowedMFFS], stat(mean) save
matrix stats = r(StatTotal)
local com_mean_pi_w = stats[1,1]
di `com_mean_pi_w'

//commercial fee mean weighted
tabstat ln_line_item_value [aw=meanallowedMFFS] if imputed_value == 0, stat(mean) save
matrix stats = r(StatTotal)
local com_mean_pi_c_w = stats[1,1]
di `com_mean_pi_c_w'

//commercial fee mean unweighted
tabstat ln_line_item_value, stat(mean) save
matrix stats = r(StatTotal)
local com_mean_pi = stats[1,1]
di `com_mean_pi'

//commercial fee mean unweighted
tabstat ln_line_item_value if imputed_value == 0, stat(mean) save
matrix stats = r(StatTotal)
local com_mean_pi_c = stats[1,1]
di `com_mean_pi_c'

//commercial fee mean unweighted and in levels
tabstat line_item_value if imputed_value == 0, stat(mean) save
matrix stats = r(StatTotal)
local com_mean_pi_c_abs = stats[1,1]
di `com_mean_pi_c_abs'

**************************************
*********** Fee w/ CPT FE ************
**************************************
// Fee index regression w/ CPT FE
_regress ln_line_item_value $RHS_fee_index, absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace pi_FE_cpt_line       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_pi' if pay_type_pooled == `x' & state == `y'
		cap replace pi_FE_cpt_line_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

// Weighted Fee index regression w/ CPT FE
_regress ln_line_item_value $RHS_fee_index [aw=meanallowedMFFS], absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace pi_W_FE_cpt_line       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_pi_w' if pay_type_pooled == `x' & state == `y'
		cap replace pi_W_FE_cpt_line_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


// Fee index regression w/ CPT FE only no imputed values
_regress ln_line_item_value $RHS_fee_index if imputed_value == 0 , absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace pi_c_FE_cpt_line       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_pi_c' if pay_type_pooled == `x' & state == `y'
		cap replace pi_c_FE_cpt_line_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

// Weighted Fee index regression w/ CPT FE Weighted only no imputed values 
_regress ln_line_item_value $RHS_fee_index [aw=meanallowedMFFS] if imputed_value == 0, absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace pi_c_W_FE_cpt_line       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_pi_c_w' if pay_type_pooled == `x' & state == `y'
		cap replace pi_c_W_FE_cpt_line_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


// Fee index regression w/ CPT FE no imputed values by specialty 
_regress ln_line_item_value $RHS_spec_fee_index if imputed_value == 0, absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
		
		forval z = 0/1 {
			
			 replace pi_c_FE_cpt_line_pcp       =   _b[`x'.pay_type_pooled#`y'.state#`z'.pcp] + `com_mean_pi_c' if pay_type_pooled == `x' & state == `y' & pcp == `z'
			 replace pi_c_FE_cpt_line_pcp_SE    =   _se[`x'.pay_type_pooled#`y'.state#`z'.pcp] if pay_type_pooled == `x' & state == `y' & pcp == `z'
		
		}
	}
}

// Fee index regression w/ CPT FE Weighted only no imputed values by specialty 
_regress ln_line_item_value $RHS_spec_fee_index [aw=meanallowedMFFS] if imputed_value == 0, absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
		
		forval z = 0/1 {
			
			 replace pi_c_W_FE_cpt_line_pcp       =   _b[`x'.pay_type_pooled#`y'.state#`z'.pcp] + `com_mean_pi_c' if pay_type_pooled == `x' & state == `y' & pcp == `z'
			 replace pi_c_W_FE_cpt_line_pcp_SE    =   _se[`x'.pay_type_pooled#`y'.state#`z'.pcp] if pay_type_pooled == `x' & state == `y' & pcp == `z'
		
		}
	}
}




// Unweighted Fee index regression w/ CPT FE Weighted only no imputed values 
_regress line_item_value $RHS_fee_index if imputed_value == 0, absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace pi_c_FE_cpt_line_abs       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_pi_c_abs' if pay_type_pooled == `x' & state == `y'
		cap replace pi_c_FE_cpt_line_abs_SE   =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}

bys payee_state_cd pay_type_pooled: gegen mean_ln_line_item_value = mean(ln_line_item_value)
bys payee_state_cd pay_type_pooled: gegen mean_line_item_value = mean(line_item_value)
bys payee_state_cd pay_type_pooled pcp: keep if _n == 1 

	
keep pi_* payee_state_cd state pay_type_pooled mean_* spec_code pcp
save `"$output/indices/fee_indices_specialty.dta"', replace 

drop spec_code pcp
bys payee_state_cd pay_type_pooled: keep if _n == 1 
save `"$output/indices/fee_indices.dta"', replace 

keep if pay_type_pooled == 2 
save `"$output/indices/fee_indices_medicaid.dta"', replace



log close 
